package com.example.satoken.common;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;


@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
//@ApiModel(description = "公共返回实体")
public class Result<T> implements Serializable {
    /** 处理成功返回状态码 */
    public static final int CODE_SUCCESS = 0;
    /** 接口调用失败返回状态码 */
    public static final int CODE_FAIL = -1;
    /** 登录失效或未登录 */
    public static final int CODE_OVERDO = -2;

  //  @ApiModelProperty("状态码 0:成功 -1:系统异常 其他请自定义")
    private int code;
  //  @ApiModelProperty("返回数据体")
    private T result;
  //  @ApiModelProperty("返回描述")
    private String message;

    /** 返回其他数据 */
  //  @ApiModelProperty("其他数据")
    private String dataTotal;

    public static <T> Result<T> newSuccess() {
        Result<T> result = new Result<>();
        result.setCode(CODE_SUCCESS);
        return result;
    }

    public static <T> Result<T> newSuccess(T res) {
        Result<T> result = new Result<>();
        result.setCode(CODE_SUCCESS);
        result.setResult(res);
        return result;
    }

    public static <T> Result<T> newFailure(T res) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setResult(res);
        return result;
    }

    public static <T> Result<T> newFailure(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> newException(Exception e) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setMessage(e.getMessage());
        return result;
    }

    public static <T> Result<T> newException(String message) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> newFailure() {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        return result;
    }

    public boolean success() {
        return code == CODE_SUCCESS;
    }

}
